package com.qfedu.shirodemo.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

/**自定义数据源
 * @author 水杯
 * @date 2020-08-09 22:55
 */
public class CustomRealmMd5 extends AuthenticatingRealm {
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       //假设数据库中获取出来的用户名和密码是
        String username="helen";
        String  password="250d6bc11dd9015cc6557a1e56741002";
        String salt=username;
        //判断用户名
//        UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;
//        String usernameInput=token.getUsername();
        String usernameInput=(String)authenticationToken.getPrincipal();
        if(!usernameInput.equals(username))
        {
            throw new UnknownAccountException("用户名不存在");
        }
//        //判断密码
//        String passwordInput=new String((char[])authenticationToken.getCredentials());
//        if(!passwordInput.equals(password))
//        {
//            throw new UnknownAccountException("密码不正确");
//        }
        //拿到认证信息
        ByteSource byteSource=ByteSource.Util.bytes(salt);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password,byteSource,this.getName());
        return info;

    }
}
